//
//  CYKTableViewController.swift
//  TestSwift
//
//  Created by 陈宇科 on 2018/11/20.
//  Copyright © 2018 陈宇科. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift

class CYKTableViewController: UIViewController {

    let disposeBag = DisposeBag.init()
    
    lazy var tableView: UITableView = {
        let tableView = UITableView.init(frame: view.bounds, style: .plain)
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellId")
        tableView.tableFooterView = UIView.init()
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        title = "RxSwift操作符";
        view.backgroundColor = UIColor.white
        view.addSubview(tableView)
        
        let items = Observable.just(["combineLatest","concat","1","1","1","1"])
        
        items.bind(to: tableView.rx.items(cellIdentifier: "cellId", cellType: UITableViewCell.self)) { (row,element,cell) in
            cell.textLabel?.text = element
        }.disposed(by: disposeBag)
        
        tableView.rx.modelSelected(String.self).subscribe(onNext: { (model) in
            self.operation(model)
        }).disposed(by: disposeBag)
        
    }
    
    func operation(_ element:String) {
        switch element {
        case "combineLatest":
            combineLatest()
            break
        case "concat":
            concat()
            break
        default:
            break
        }
    }

    /// 当多个 Observables 中任何一个发出一个元素，就发出一个元素。这个元素是由这些 Observables 中最新的元素，通过一个函数组合起来的
    func combineLatest() {
        let first = PublishSubject<String>.init()
        let second = PublishSubject<String>.init()
        
        Observable.combineLatest(first,second) { $0 + $1 }
            .subscribe(onNext: { print($0)})
            .disposed(by: disposeBag)
        first.onNext("1")
        second.onNext("A")
        first.onNext("2")
        second.onNext("B")
        second.onNext("C")
        second.onNext("D")
        first.onNext("3")
        first.onNext("4")
    }
    
    /// 让两个或多个 Observables 按顺序串连起来
    func concat() {
        let disposeBag = DisposeBag()
        
        let subject1 = BehaviorSubject(value: "🍎")
        let subject2 = BehaviorSubject(value: "🐶")
        
        let behaviorRelay = BehaviorRelay.init(value: subject1)
        
        behaviorRelay.asObservable()
            .concat()
            .subscribe { print($0) }
            .disposed(by: disposeBag)
        
        subject1.onNext("🍐")
        subject1.onNext("🍊")
        
        behaviorRelay.accept(subject2)
        
        subject2.onNext("I would be ignored")
        subject2.onNext("🐱")
        
        subject1.onCompleted()
        subject2.onNext("🐭")
    }
    
    func concatMap() {
        let disposeBag = DisposeBag()
        
        let subject1 = BehaviorSubject(value: "🍎")
        let subject2 = BehaviorSubject(value: "🐶")
        
        let behaviorRelay = BehaviorRelay.init(value: subject1)
        
        behaviorRelay.asObservable()
            .concatMap { $0 }
            .subscribe { print($0) }
            .disposed(by: disposeBag)
        
        subject1.onNext("🍐")
        subject1.onNext("🍊")
        
        behaviorRelay.accept(subject2)
        
        subject2.onNext("I would be ignored")
        subject2.onNext("🐱")
        
        subject1.onCompleted()
        
        subject2.onNext("🐭")
    }
}
